﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using AmuTools;
using app.Api;

namespace app.Models
{
    [Model(TableName = "t_abutment", PrimaryKey = "id", IdentityInsert = false, BaseID = "1000000000")]
    public class Abutment
    {
        [Field(Description = "唯一标识符")]
        public int id { get; set; }


        [Field(DataType = "nchar(50)", Description = "编号=时间戳+服务方ID+需求方ID")]
        public string number { get; set; }


        [Field(Description = "发起者：需求方demander，服务方provider")]
        public string sponsor { get; set; }


        [Field(Description = "标题")]
        public string title { get; set; }


        [Field(DataType = "text", Description = "描述")]
        public string description { get; set; }


        [Field(Description = "需求方的ID")]
        public int demand_member_id { get; set; }

        
        [Field(Description = "需求方企业名")]
        public string demand_enterprise { get; set; }


        private Member _demand_member = null;
        [Field(Storageable = false, Description = "需求方")]
        public Member demand_member
        {
            get
            {
                if (_demand_member == null)
                {
                    _demand_member = G.DB.GetById<Member>(demand_member_id);
                }
                return _demand_member;
            }
        }


        [Field(Description = "服务方的ID")]
        public int service_member_id { get; set; }

        
        [Field(Description = "服务方机构名")]
        public string service_enterprise { get; set; }


        private Member _service_member = null;
        [Field(Storageable = false, Description = "服务方")]
        public Member service_member
        {
            get
            {
                if (_service_member == null)
                {
                    _service_member = G.DB.GetById<Member>(service_member_id);
                }
                return _service_member;
            }
        }


        [Field(Description = "需求方项目ID，可能没有")]
        public int demand_id { get; set; }


        [Field(Description = "需求方项目名称")]
        public string demand_name { get; set; }


        private DemandItem _demand = null;
        [Field(Storageable = false, Description = "需求方项目")]
        public DemandItem demand
        {
            get
            {
                if (_demand == null)
                {
                    _demand = G.DB.GetById<DemandItem>(demand_id);
                }
                return _demand;
            }
        }


        [Field(Description = "服务方服务ID")]
        public int service_id { get; set; }


        [Field(Description = "服务方服务名称")]
        public string service_name { get; set; }


        private ServiceItem _service = null;
        [Field(Storageable = false, Description = "服务方的服务")]
        public ServiceItem service
        {
            get
            {
                if (_service == null && service_id != 0)
                {
                    _service = G.DB.GetById<ServiceItem>(service_id);
                }
                return _service;
            }
        }


        [Field(DataType = "char(13)", Description = "创建时间")]
        public string create_time { get; set; }


        [Field(Description = "需求方对接状态：0未操作，2同意，3完成，4取消")]
        public int demand_status { get; set; }


        [Field(Description = "服务方对接状态：0未操作，2同意，3完成，4取消")]
        public int service_status { get; set; }


        [Field(DataType = "text", Description = "需求方给服务方的评价")]
        public string serviceReason { get; set; }


        [Field(DataType = "text", Description = "服务方给需求方的评价")]
        public string demandReason { get; set; }


        [Field(DataType = "float", Description = "需求方给服务方的评分")]
        public double serviceRate { get; set; }


        [Field(DataType = "float", Description = "服务方给需求方的评分")]
        public double demandRate { get; set; }


        [Field(Storageable = false, Description = "合作流程的步骤，基于服务方服务的步骤")]
        public List<Step> steps
        {
            get
            {
                return Step.GetSteps(service_id);
            }
        }


        [Field(Storageable = false, Description = "合作流程步骤的相关信息，如当前所处步骤、状态等")]
        public StepAbutmentData step_abutment_data { // 当前步骤序号
            get
            {
                return G.DB.GetById<StepAbutmentData>(id);
            }
        }


        [Field(Webable = false, Description = "是否已被删除")]
        public int is_delete { get; set; }

        [Field(Description = "拒绝原因")]
        public string refuse_reason { get; set; }


        [Field(Description = "合同，文件形式，可以是图片或者文档。以后可能有多个文件的情况，所以这里写的是合同文件的id")]
        public string contract_ids { get; set; }


        [Field(Storageable = false, Description = "合同文件数组")]
        public List<UploadedFile> contract
        {
            get
            {
                if (contract_ids == "" || contract_ids == null) return new List<UploadedFile>();
                else return G.DB.Get<UploadedFile>(string.Format("{0} in ({1})", G.DB.GetPrimaryKey<UploadedFile>(), contract_ids)).GetFirstTableList<UploadedFile>();
            }
        }
    }

    class SEND_DIRECTION
    {
        public static string Receive = "receive"; // 接收
        public static string Deliver = "deliver"; // 投递
    }

    enum ABUTMENT_STATUS
    {
        NONE = 0, // 未操作
        INTERVIEW = 1, // 约谈
        AGREE = 2, // 同意
        FINISH = 3, // 完成
        CANCEL = 4, // 取消
    }
}
